package com.ruoyi.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.im.entity.YytImJoinup;
import com.ruoyi.common.im.entity.YytImMessage;
import com.ruoyi.common.im.service.YytImJoinupService;
import com.ruoyi.common.im.service.YytImMessageService;
import com.ruoyi.common.pms.entity.YytShopMember;
import com.ruoyi.common.pms.service.YytShopMemberService;
import com.ruoyi.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author MrPing
 * @since 2022-08-23
 */
@RestController
@RequestMapping("/joinup")
public class YytImJoinupController {

    @Autowired
    private YytImJoinupService yytImJoinupService;

    @Autowired
    private YytImMessageService yytImMessageService;

    @Autowired
    private YytShopMemberService yytShopMemberService;

    /**
     * 客服初次接入用户消息
     * memberId： 对接用户id
     * serviceId： 当前客服id
     */
    @PostMapping("/service")
    public ResponseResult service(@RequestBody YytImJoinup yytImJoinup) {
        boolean save = yytImJoinupService.save(yytImJoinup);
        if(save){
            QueryWrapper<YytShopMember> wrapper = new QueryWrapper<>();
            String[] authList = new String[]{"1", "3"};
            wrapper.eq("member_id", yytImJoinup.getServiceId()).eq("pass_status", 1).in("auth_status", authList);
            YytShopMember one = yytShopMemberService.getOne(wrapper, false);
            updateMessage(yytImJoinup.getMemberId(), one.getShopId(), yytImJoinup.getServiceId());
        }
        return ResponseResult.success(save);
    }


    /**
     * 客服转接
     * @param memberId 用户编号
     * @param serviceId 客服编号
     * @param turnServiceId 转接人编号
     * @return
     */
    @GetMapping("/switchover")
    public ResponseResult switchover(@RequestParam("memberId") String memberId,
                                     @RequestParam("serviceId") String serviceId,
                                     @RequestParam("turnServiceId") String turnServiceId) {
        UpdateWrapper<YytImJoinup> wrapper = new UpdateWrapper<>();
        wrapper.eq("member_id", memberId).eq("service_id", turnServiceId).eq("status", 0).set("status", 2);
        boolean update = yytImJoinupService.update(wrapper);
        if(update) {
            YytImJoinup yytImJoinup = new YytImJoinup();
            yytImJoinup.setMemberId(memberId);
            yytImJoinup.setServiceId(serviceId);
            yytImJoinup.setTurnServiceId(turnServiceId);
            yytImJoinup.setCreateTime(LocalDateTime.now());
            yytImJoinup.setType(1);
            yytImJoinupService.save(yytImJoinup);
            updateMessage(memberId, turnServiceId, serviceId);
        }
        return ResponseResult.success(false);
    }


    /**
     * 修改消息发送人
     * @param fromId  发送人
     * @param toId    接收人
     * @param turnId  转接人
     * @return
     */
    public void updateMessage(String fromId, String toId, String turnId) {
        QueryWrapper<YytImMessage> wrapper = new QueryWrapper<>();
        wrapper.eq("from_id", fromId).eq("to_id", toId);
        List<YytImMessage> list = yytImMessageService.list(wrapper);
        for (YytImMessage yytImMessage : list) {
            yytImMessage.setToId(turnId);
        }
        yytImMessageService.updateBatchById(list);
    }

}
